<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="学茶方式" prop="studyId">
        <el-select v-model="queryParams.studyId" placeholder="请选择学茶方式" clearable>
          <el-option
            v-for="dict in dict.type.tea_study"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="文章标题" prop="articleTitle">
        <el-input
          v-model="queryParams.articleTitle"
          placeholder="请输入文章标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="作者" prop="articleAuthor">
        <el-input
          v-model="queryParams.articleAuthor"
          placeholder="请输入作者"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="日期">
        <el-date-picker
          v-model="daterangeArticleDate"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
<!--      <el-form-item label="阅读量" prop="articleRead">-->
<!--        <el-input-->
<!--          v-model="queryParams.articleRead"-->
<!--          placeholder="请输入阅读量"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="点赞量" prop="articleZan">-->
<!--        <el-input-->
<!--          v-model="queryParams.articleZan"-->
<!--          placeholder="请输入点赞量"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['tea:article:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['tea:article:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['tea:article:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['tea:article:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="articleList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="文章id" align="center" prop="articleId" />
      <el-table-column label="学茶方式" align="center" prop="studyId">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.tea_study" :value="scope.row.studyId"/>
        </template>
      </el-table-column>
      <el-table-column label="文章标题" align="center" prop="articleTitle" />
      <el-table-column label="作者" align="center" prop="articleAuthor" />
      <el-table-column label="日期" align="center" prop="articleDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.articleDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="阅读量" align="center" prop="articleRead" />
      <el-table-column label="点赞量" align="center" prop="articleZan" />
<!--      <el-table-column label="文章段落1" align="center" prop="articlePara1" />-->
<!--      <el-table-column label="文章段落2" align="center" prop="articlePara2" />-->
<!--      <el-table-column label="文章段落3" align="center" prop="articlePara3" />-->
<!--      <el-table-column label="文章段落4" align="center" prop="articlePara4" />-->
<!--      <el-table-column label="文章段落8" align="center" prop="articlePara5" />-->
<!--      <el-table-column label="文章段落6" align="center" prop="articlePara6" />-->
<!--      <el-table-column label="文章段落7" align="center" prop="articlePara7" />-->
<!--      <el-table-column label="文章段落8" align="center" prop="articlePara8" />-->
<!--      <el-table-column label="文章段落9" align="center" prop="articlePara9" />-->
<!--      <el-table-column label="文章段落10" align="center" prop="articlePara10" />-->
<!--      <el-table-column label="文章图片1" align="center" prop="articleImg" width="100">-->
<!--        <template slot-scope="scope">-->
<!--          <image-preview :src="scope.row.articleImg" :width="50" :height="50"/>-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--      <el-table-column label="文章图片2" align="center" prop="articleImg1" width="100">-->
<!--        <template slot-scope="scope">-->
<!--          <image-preview :src="scope.row.articleImg1" :width="50" :height="50"/>-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['tea:article:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['tea:article:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改文章对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="学茶方式" prop="studyId">
          <el-select v-model="form.studyId" placeholder="请选择学茶方式">
            <el-option
              v-for="dict in dict.type.tea_study"
              :key="dict.value"
              :label="dict.label"
:value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="文章标题" prop="articleTitle">
          <el-input v-model="form.articleTitle" placeholder="请输入文章标题" />
        </el-form-item>
        <el-form-item label="作者" prop="articleAuthor">
          <el-input v-model="form.articleAuthor" placeholder="请输入作者" />
        </el-form-item>
        <el-form-item label="日期" prop="articleDate">
          <el-date-picker clearable
            v-model="form.articleDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="阅读量" prop="articleRead">
          <el-input v-model="form.articleRead" placeholder="请输入阅读量" />
        </el-form-item>
        <el-form-item label="点赞量" prop="articleZan">
          <el-input v-model="form.articleZan" placeholder="请输入点赞量" />
        </el-form-item>
        <el-form-item label="文章段落1" prop="articlePara1">
          <el-input v-model="form.articlePara1" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="文章段落2" prop="articlePara2">
          <el-input v-model="form.articlePara2" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="文章段落3" prop="articlePara3">
          <el-input v-model="form.articlePara3" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="文章段落4" prop="articlePara4">
          <el-input v-model="form.articlePara4" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="文章段落8" prop="articlePara5">
          <el-input v-model="form.articlePara5" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="文章段落6" prop="articlePara6">
          <el-input v-model="form.articlePara6" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="文章段落7" prop="articlePara7">
          <el-input v-model="form.articlePara7" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="文章段落8" prop="articlePara8">
          <el-input v-model="form.articlePara8" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="文章段落9" prop="articlePara9">
          <el-input v-model="form.articlePara9" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="文章段落10" prop="articlePara10">
          <el-input v-model="form.articlePara10" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="文章图片1">
          <image-upload v-model="form.articleImg"/>
        </el-form-item>
        <el-form-item label="文章图片2">
          <image-upload v-model="form.articleImg1"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listArticle, getArticle, delArticle, addArticle, updateArticle } from "@/api/tea/article";

export default {
  name: "Article",
  dicts: ['tea_study'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 文章表格数据
      articleList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 文章图片2时间范围
      daterangeArticleDate: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        studyId: null,
        articleTitle: null,
        articleAuthor: null,
        articleDate: null,
        articleRead: null,
        articleZan: null,
        articlePara1: null,
        articlePara2: null,
        articlePara3: null,
        articlePara4: null,
        articlePara5: null,
        articlePara6: null,
        articlePara7: null,
        articlePara8: null,
        articlePara9: null,
        articlePara10: null,
        articleImg: null,
        articleImg1: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        studyId: [
          { required: true, message: "学茶方式不能为空", trigger: "change" }
        ],
        articleTitle: [
          { required: true, message: "文章标题不能为空", trigger: "blur" }
        ],
        articleAuthor: [
          { required: true, message: "作者不能为空", trigger: "blur" }
        ],
        articleDate: [
          { required: true, message: "日期不能为空", trigger: "blur" }
        ],
        articleRead: [
          { required: true, message: "阅读量不能为空", trigger: "blur" }
        ],
        articleZan: [
          { required: true, message: "点赞量不能为空", trigger: "blur" }
        ],
        articlePara1: [
          { required: true, message: "文章段落1不能为空", trigger: "blur" }
        ],
        articlePara2: [
          { required: true, message: "文章段落2不能为空", trigger: "blur" }
        ],
        articlePara3: [
          { required: true, message: "文章段落3不能为空", trigger: "blur" }
        ],
        articlePara4: [
          { required: true, message: "文章段落4不能为空", trigger: "blur" }
        ],
        articlePara5: [
          { required: true, message: "文章段落8不能为空", trigger: "blur" }
        ],
        articlePara6: [
          { required: true, message: "文章段落6不能为空", trigger: "blur" }
        ],
        articlePara7: [
          { required: true, message: "文章段落7不能为空", trigger: "blur" }
        ],
        articlePara8: [
          { required: true, message: "文章段落8不能为空", trigger: "blur" }
        ],
        articlePara9: [
          { required: true, message: "文章段落9不能为空", trigger: "blur" }
        ],
        articlePara10: [
          { required: true, message: "文章段落10不能为空", trigger: "blur" }
        ],
        articleImg: [
          { required: true, message: "文章图片1不能为空", trigger: "blur" }
        ],
        articleImg1: [
          { required: true, message: "文章图片2不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询文章列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeArticleDate && '' != this.daterangeArticleDate) {
        this.queryParams.params["beginArticleDate"] = this.daterangeArticleDate[0];
        this.queryParams.params["endArticleDate"] = this.daterangeArticleDate[1];
      }
      listArticle(this.queryParams).then(response => {
        this.articleList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        articleId: null,
        studyId: null,
        articleTitle: null,
        articleAuthor: null,
        articleDate: null,
        articleRead: null,
        articleZan: null,
        articlePara1: null,
        articlePara2: null,
        articlePara3: null,
        articlePara4: null,
        articlePara5: null,
        articlePara6: null,
        articlePara7: null,
        articlePara8: null,
        articlePara9: null,
        articlePara10: null,
        articleImg: null,
        articleImg1: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeArticleDate = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.articleId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加文章";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const articleId = row.articleId || this.ids
      getArticle(articleId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改文章";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.articleId != null) {
            updateArticle(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addArticle(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const articleIds = row.articleId || this.ids;
      this.$modal.confirm('是否确认删除文章编号为"' + articleIds + '"的数据项？').then(function() {
        return delArticle(articleIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('tea/article/export', {
        ...this.queryParams
      }, `article_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
